/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sdr.elementos;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;
import sdr.HibernateUtil;

/**
 *
 * @author stevenlizarazo
 */
@Entity
public 
 class usuario implements Serializable {

    @Id
@GeneratedValue(strategy = GenerationType.AUTO)

    private int id=0;
private String nombres="";
private String correo="";
private String password="";
        @ManyToOne
private perfiles perfil;
    
public String getNombres(){ return nombres;}
public String getCorreo(){  return correo;}
public String getPassword(){return password;}

   public int getId(){return id;}

public void setNombres(String n){  nombres=n;}
public void setCorreo(String c){  correo=c;}
public void setPassword(String p){password=p;}
public void setId(int i){id=i;}

//Expert
public static usuario valid(String correo, String pass){
 Session hsession = HibernateUtil.getSessionFactory().getCurrentSession();
        hsession.beginTransaction();

 List result = hsession.createCriteria(usuario.class)
       .add( Restrictions.eq("correo", correo) )
     //  .add(Restrictions.eq("password", pass))
         .list();
        if (result.size() > 0) {
            Iterator it = result.iterator();
            
             return (usuario) it.next();
            }
        
    
return null;
}

public void save(){

 Session hsession = HibernateUtil.getSessionFactory().getCurrentSession();
        hsession.beginTransaction();

        hsession.save(this);

        hsession.getTransaction().commit();  
}

public void update(){

 Session hsession = HibernateUtil.getSessionFactory().getCurrentSession();
        hsession.beginTransaction();

        hsession.update(this);

        hsession.getTransaction().commit();  
}



public void delete(){

 Session hsession = HibernateUtil.getSessionFactory().getCurrentSession();
        hsession.beginTransaction();

        hsession.delete(this);

        hsession.getTransaction().commit();  
}
}

